<template>
  <div>
    <el-upload
      class="pic-uploader-component"
      :action="$http.adornUrl('/admin/file/upload/element')"
      :headers="{Authorization: $cookie.get('Authorization')}"
      :show-file-list="false"
      :on-progress="uploadVideoProcess"
      :on-success="handleUploadSuccess"
      :before-upload="beforeAvatarUpload">
      <video v-if="value"
             id="videoPlay"
             v-bind:src="resourcesUrl + value"
             class="video"
             controls="controls">
      </video>
      <i v-else-if="(value ==''||value == null)&&!videoFlag" class="el-icon-plus pic-uploader-icon"></i>
      <el-progress v-if="videoFlag == true"
                   type="circle"
                   v-bind:percentage="videoUploadPercent"
                   class="progress">
      </el-progress>
    </el-upload>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        // 是否显示进度条
        videoFlag: false,
        // 进度条的进度，
        videoUploadPercent: '',
        videoDuration: 0,
        resourcesUrl: window.SITE_CONFIG.resourcesUrl
      }
    },
    props: {
      value: {
        default: '',
        type: String
      }
    },
    methods: {
      uploadVideoProcess (event, file, fileList) {
        this.videoFlag = true
        this.videoUploadPercent = file.percentage.toFixed(0) * 1;
      },
      // 视频上传
      handleUploadSuccess (response, file, fileList) {
        var that = this
        this.videoFlag = false
        this.videoUploadPercent = 0
        this.$emit('input', file.response)
        setTimeout(function () {
          that.videoDuration = document.getElementById('videoPlay').duration
          that.$emit('videoDurationChange', {videoDuration: that.videoDuration})
        }, 1000)
      },
      // 限制视频上传大小
      beforeAvatarUpload (file) {
        let fileSize = file.size / 1024 / 1024 < 50
        if (['video/mp4', 'video/ogg', 'video/flv', 'video/avi', 'video/wmv', 'video/rmvb', 'video/mov'].indexOf(file.type) == -1) {
          this.$message.error('上传正确的格式!')
          return false
        }
        if (!fileSize) {
          this.$message.error('视频大小不能超过50MB!')
          return false
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .pic-uploader-component .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    .pic-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 148px;
      height: 148px;
      line-height: 148px;
      text-align: center;
    }
    .video {
      width: 148px;
      height: 148px;
      display: block;
    }
    .progress{
      width: 128px;
      height: 128px;
      display: block;
      margin-top:10px;
      margin-left:10px;
    }
  }
  .pic-uploader-component .el-upload:hover {
    border-color: #409EFF;
  }

</style>

